<template>
  <div class="kong">
    <el-dialog
      title="关联会员账号"
      v-model="addAccountVisible"
      width="40%"
      :close-on-click-modal="false"
      :before-close="handelClose"
      center
    >
      <div>
        <el-form
          ref="addAccountRef"
          :rules="rules"
          :model="row"
          label-width="120px"
        >
          <el-form-item label="内容" prop="content">
            <el-input :value="row.content" v-model="row.content"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input :value="row.address" v-model="row.address"></el-input>
          </el-form-item>
          <el-form-item label="账号类型" prop="accountType">
            <el-select
              :value="row.accountType"
              v-model="row.accountType"
              placeholder="请选择计划类型"
            >
              <el-option
                v-for="item in accountTypeParams"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                default-first-option
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="时间范围" prop="startTime">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                style="margin-right: 10px; margin-bottom: 10px"
                v-model="row.startTime"
                type="date"
                placeholder="开始时间"
              />
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                v-model="row.endTime"
                type="date"
                placeholder="结束时间"
              />
            </el-form-item>
          </el-form-item>

          <el-form-item label="账号" prop="accountNo">
            <el-input :value="row.accountNo" v-model="row.accountNo"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input :value="row.password" v-model="row.password"></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input :value="row.remark" v-model="row.remark"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="changeStatus(!addAccountVisible, addAccountRef)"
            >取 消</el-button
          >
          <el-button type="primary" @click="submitAddIt(addAccountRef)"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs, onMounted } from 'vue'
import { FormRules, FormInstance } from 'element-plus'
import { accountRows, countRows, memberAdd, memberGetOne } from '@/api/expenses'
import { selectDetail } from '@/constants/commonSelect'
import { ElMessage } from 'element-plus'
import { getGroupDetail } from '@/api/config'

let row: accountRows = reactive({
  id: '',
  countId: '',
  content: '',
  remark: '',
  dateTime: [],
  startTime: '',
  endTime: '',
  accountNo: '',
  password: '',
  accountType: 1,
  address: '',
})

// 初始化查询
onMounted(() => {
  getAccountType()
})

const getAccountType = async () => {
  getGroupDetail('constgroup_account_type').then((res: any) => {
    accountTypeParams.value = res
  })
}

const addAccountRef = ref<FormInstance>()

interface Props {
  addAccountVisible?: Boolean
}

const myProps = defineProps<Props>()

const { addAccountVisible } = toRefs(myProps)

const emit = defineEmits(['addAccountConfig'])

const accountTypeParams = ref<selectDetail[]>([])

const changeStatus = (status: boolean, formEl: FormInstance | undefined) => {
  emit('addAccountConfig', status)
  resetFields(formEl)
}

const handelClose = (done: () => void, formEl: FormInstance | undefined) => {
  changeStatus(!addAccountVisible, formEl)
  done()
}

const resetFields = (formEl: FormInstance | undefined) => {
  if (formEl) {
    formEl.resetFields()
  } else {
    addAccountRef.value?.resetFields()
  }
  row.accountType = 1
  row.content = ''
  row.remark = ''
  row.startTime = ''
  row.endTime = ''
  row.accountNo = ''
  row.password = ''
  row.dateTime = []
  row.address = ''
}

const rules = reactive<FormRules>({
  content: [
    {
      required: true,
      message: '请输入内容',
      trigger: 'change',
    },
  ],
  accountNo: [
    {
      required: true,
      message: '请输入账号',
      trigger: 'change',
    },
  ],
})

const submitAddIt = async (formEl: FormInstance | undefined) => {
  console.log('submitAddIt')
  if (!formEl) {
    return
  }

  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      memberAdd(row).then((res: any) => {
        if (res.data) {
          changeStatus(!addAccountVisible, formEl)
          ElMessage.success('添加成功')
        }
      })
    }
  })
}

const initsObj = (rows: countRows) => {
  row.countId = rows.id
  row.remark = rows.remark
  row.content = rows.content
  row.startTime = rows.planStartTime
  row.endTime = rows.planFinishTime

  memberGetOne(rows.id).then((res: any) => {
    if (res.data) {
      row.accountNo = res.data.accountNo
      row.password = res.data.password
      row.accountType = res.data.accountType
      row.remark = res.data.remark
      row.content = res.data.content
      row.startTime = res.data.startTime
      row.endTime = res.data.endTime
      row.address = res.data.address
    }
  })
  console.log('wend test223:' + JSON.stringify(row))
}

defineExpose({
  initsObj,
})
</script>
```
